﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using System.Drawing;

namespace smallDeimos.Game
{
    public partial class Planets : System.Web.UI.Page
    {
        int UserId;
        int farms, factories, cybs, labs, pop;
        double pp;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            int sId = 0;
            int pId = 0;
            GridViewRow row = GridView1.SelectedRow;
            try
            {
                sId = int.Parse(row.Cells[0].Text);
                pId = int.Parse(row.Cells[1].Text);
                Response.Redirect("Planet.aspx?sId=" + sId + "&pId=" + pId);
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder("_");
                foreach (TableCell c in row.Cells)
                    sb.Append(c.Text + "_");
                throw new Exception(sb.ToString(), ex);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            StoredProcedures.SpendAll(UserId);
            Response.Redirect("Planets.aspx");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                farms += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Farms"));
                factories += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Factories"));
                cybs += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Cybernets"));
                labs += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Labs"));
                pp += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ProductionPoints"));
                pop += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PopulationLevel"));

                int fId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "FleetOwnerID"));
                if (fId != UserId && fId != 0)
                    e.Row.BackColor = Color.DarkRed;
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Sum:";
                e.Row.Cells[1].Text = farms.ToString();
                e.Row.Cells[2].Text = factories.ToString();
                e.Row.Cells[3].Text = cybs.ToString();
                e.Row.Cells[4].Text = labs.ToString();
                e.Row.Cells[6].Text = pop.ToString();
                e.Row.Cells[7].Text = ((int)pp).ToString();
                e.Row.BackColor = Color.DarkBlue;
                e.Row.Font.Bold = true;
            }
        }
    }
}
